import Vue from 'vue'
import Router from 'vue-router'
import login from '@/components/login'
import index from '@/components/index'
import list from '@/components/list'
import detail from '@/components/detail'
import recordList from '@/components/recordList'
import my from '@/components/my'
import edit from '@/components/edit'
import renewal from '@/components/renewal'
import paySuccess from '@/components/paySuccess'
import mlogin from '@/components/mlogin'
import mindex from '@/components/mindex'
import mverification from '@/components/mverification'
import mopencard from '@/components/mopencard'
import mmy from '@/components/mmy'
import magree from '@/components/magree'
import mlist from '@/components/mlist'
import mplist from '@/components/mplist'
import error from '@/components/error'
import getUrlParam from '../../store/modules/getUrlParam'
import cookie from '../../store/modules/cookie'
import http from '../../store/modules/http';
import { MessageBox } from 'mint-ui';
Vue.use(Router)

const router = new Router({
  // base: '/dist/',
  // mode: 'history',
  routes: [
    {
      path: '/login',
      name: 'login',
      component: login
    },
    {
      path: '/',
      name: 'index',
      component: index,
      meta:{
        auth:true
      }
    },
    {
      path: '/list',
      name: 'list',
      component: list,
      // meta:{
      //   keepAlive:true
      // }
    },
    {
      path: '/detail',
      name: 'detail',
      component: detail
    },
    {
      path: '/recordList',
      name: 'recordList',
      component: recordList,
      // meta:{
      //   keepAlive:true
      // }
    },
    {
      path: '/my',
      name: 'my',
      component: my
    },
    {
      path: '/edit',
      name: 'edit',
      component: edit
    },
    {
      path: '/renewal',
      name: 'renewal',
      component: renewal
    },
    {
      path: '/paySuccess',
      name: 'paySuccess',
      component: paySuccess
    },
    {
      path: '/mlogin',
      name: 'mlogin',
      component: mlogin
    },
    {
      path: '/mindex',
      name: 'mindex',
      component: mindex,
      meta:{
        mauth:true
      }
    },
    {
      path:'/mverification',
      name:'mverification',
      component:mverification
    },
    {
      path:'/mopencard',
      name:'mopencard',
      component:mopencard
    },
    {
      path:'/mmy',
      name:'mmy',
      component:mmy
    },
    {
      path:'/magree',
      name:'magree',
      component:magree
    },
    {
      path:'/mlist',
      name:'mlist',
      component:mlist
    },
    {
      path:'/mplist',
      name:'mplist',
      component:mplist
    },
    {
      path:'/error',
      name:'error',
      component:error
    }
  ],
  // scrollBehavior(to, from, savedPosition) {
  //   if (savedPosition) {
  //     return savedPosition
  //   } else {
  //     if (from.meta.keepAlive) {
  //       from.meta.savedPosition = document.body.scrollTop
  //     }
  //     return { x: 0, y: to.meta.savedPosition || 0 }
  //   }
  // },
})
router.beforeEach(function (to, from, next) {
  if (to.meta.auth){
      let openid = cookie.getCookie('openid')
      if(openid){
        http.get('/user/index/index',{openid:openid}).then((res)=>{
            if (res.code == -1) {
              if(res.data.flag==0){
                router.push({
                  name: 'error',
                  params:{
                    msg:res.msg
                  }
                })
                  //获取用户信息失败
              }else{
                //获取用户信息成功
                router.push({name: 'login'})
              }
            }else if(res.code == 0){
              next()
            }
        })
      }else{
        const getCode = getUrlParam.getParam('code');
        if(getCode){
          let getRef = getUrlParam.getParam('ref_code')?getUrlParam.getParam('ref_code'):0;
          http.get('/user/index/index',{code:getCode}).then((res)=>{
              if (res.code == -1) {
                if(res.data.flag==0){
                  router.push({
                    name: 'error',
                    params:{
                      msg:res.msg
                    }
                  })
                    //获取用户信息失败
                }else{
                  //获取用户信息成功
                  cookie.setCookie('openid',res.data.openid)
                  router.push({name: 'login',params:{ref_code:getRef}})
                }
              }else if(res.code == 0){
                cookie.setCookie('openid',res.data.openid)
                next()
              }
          })
        }else{
          var pageUrl = window.location.href.replace(/[/]/g, "%2f").replace(/[:]/g, "%3a").replace(/[#]/g, "%23").replace(/[&]/g, "%26").replace(/[=]/g, "%3d");
          var url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + "wx1b5c6a97e8b6fbbc" + "&redirect_uri=" + pageUrl + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
          window.location.href = url;
        }
      }
  }else if(to.meta.mauth){
    //验证商户注册begin
    let openid = cookie.getCookie('openid')  
    if(openid){
      http.get('/merchant/index/index',{openid:openid}).then((res)=>{
        if(res.code == 0){
          if(res.data.isRreg==-1){
            router.push({name: 'mlogin'})
          }else{
            if(res.data.status !=2){
              router.push({name: 'error',params:{msg:res.msg}})
            }else{
              next()
            }
          }
        }else{
          MessageBox(res.msg)
        }
      })
    }else{
      const getCode = getUrlParam.getParam('code');
      if(getCode){
        http.get('/merchant/index/index',{code:getCode}).then((res)=>{
            cookie.setCookie('openid',res.data.openid)
            if(res.code == 0){
              if(res.data.isRreg==-1){
                router.push({name: 'mlogin'})
              }else{
                if(res.data.status !=2){
                  router.push({name: 'error',params:{msg:res.msg}})
                }else{
                
                  next()
                }
              }
            }else{
              MessageBox(res.msg)
            }
        })
      }else{
        var pageUrl = window.location.href.replace(/[/]/g, "%2f").replace(/[:]/g, "%3a").replace(/[#]/g, "%23").replace(/[&]/g, "%26").replace(/[=]/g, "%3d");
        var url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + "wx1b5c6a97e8b6fbbc" + "&redirect_uri=" + pageUrl + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
        window.location.href = url;
      }
    }
    //end
  }else{
    next();
  }
    
});
export default router
